//
//  MKLineComicViewController.m
//  ManKeCartoon
//
//  Created by 邹功梁 on 15/10/21.
//  Copyright © 2015年 ManKe. All rights reserved.
//

#import "MKLineComicViewController.h"
#import "MKBaseHeader.h"
#import "MKLineComicTableViewCell.h"
#import "MKLineComicDetailViewController.h"
@interface MKLineComicViewController ()<UITableViewDataSource, UITableViewDelegate>

{
    __block NSInteger _upDateCount;
}

@end

@implementation MKLineComicViewController
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        _lightComicDataArray = [[NSMutableArray alloc] init];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self customNavBar];
    
    [self getData];
}
#pragma mark - CustomNavBar
- (void)customNavBar
{
    _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backButton setFrame:CGRectMake(10, 23, 30, 30)];
    [_backButton setImage:[UIImage imageNamed:@"iconfont-arrowleft.png"] forState:UIControlStateNormal];
    [_backButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.navBarView addSubview:_backButton];
}
#pragma mark - 刷新
#pragma mark - 下拉刷新
- (void)addHeaderData
{
    __weak __typeof(self) weakSelf = self;
    [_lineTableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf getData:1 isDown:YES];
    }];
}
#pragma mark - 上拉加载
- (void)addFooterData
{
    __weak __typeof(self) weakSelf = self;
    [_lineTableView addLegendFooterWithRefreshingBlock:^{
        [weakSelf getData:_upDateCount isDown:NO];
    }];
}
#pragma mark - 加载方法
- (void)getData:(NSInteger)data isDown:(BOOL)isDown
{
    [AFNETWORK getDataWithNoCacheString:[NSString stringWithFormat:@"http://m.ac.qq.com/LightComic/getComicList?t=1445427883983&page=%ld&pagesize=10", data] block:^(id block) {
        
        if (isDown) {
            [_lightComicDataArray removeAllObjects];
            _upDateCount = 2;
        }
        for (NSDictionary *dic in [block objectForKey:@"data"]) {
            MKLightComicModel *lightComicModel = [[MKLightComicModel alloc] initWithDictionary:dic];
            lightComicModel.comicID = [dic objectForKey:@"id"];
            [_lightComicDataArray addObject:lightComicModel];
        }
        
        [_lineTableView reloadData];
        if (!isDown) {
            _upDateCount ++;
        }
        [_lineTableView.header endRefreshing];
        [_lineTableView.footer endRefreshing];
        
    }];
}
#pragma mark - 返回按钮点击方法
- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - GetData
- (void)getData
{
//    http://m.ac.qq.com/LightComic/getComicList?t=1445427883983&page=1&pagesize=10
    [AFNETWORK getDataWithNoCacheString:@"http://m.ac.qq.com/LightComic/getComicList?t=1445427883983&page=1&pagesize=10" block:^(id block) {
        
        for (NSDictionary *dic in [block objectForKey:@"data"]) {
            MKLightComicModel *lightComicModel = [[MKLightComicModel alloc] initWithDictionary:dic];
            lightComicModel.comicID = [dic objectForKey:@"id"];
            [_lightComicDataArray addObject:lightComicModel];
        }
        
        [self setUpTableView];
        _upDateCount = 2;
    }];
}
#pragma mark - SetUpTableView
- (void)setUpTableView
{
    _lineTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    _lineTableView.dataSource = self;
    _lineTableView.delegate = self;
    _lineTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_lineTableView];
    
    [self addHeaderData];
    [self addFooterData];
}
#pragma mark - TableView DataSource&Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _lightComicDataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (SCREEN_HEIGHT - 64) / 3;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"reusableIdentifier";
    MKLineComicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[MKLineComicTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    MKLightComicModel *model = [_lightComicDataArray objectAtIndex:indexPath.row];
    [cell setLightComicModel:model];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MKLightComicModel *lightModel = [_lightComicDataArray objectAtIndex:indexPath.row];
    MKLineComicDetailViewController *detail = [[MKLineComicDetailViewController alloc] init];
    detail.lightComicModel = lightModel;
    [self.navigationController pushViewController:detail animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
